package com.tvsinfo.jdk.demo.case3_reference;

import com.tvsinfo.jdk.demo.case2_function_interface.dto.Car;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * 引用
 */
public class ReferenceDemo {


    public static void main(String[] args) {
        List<Car> cars = getList();

        //实例1、类方法引用
        List<Integer> collect = cars.stream().map(Car::getPrice).collect(Collectors.toList());

        //实例2、实例方法引用
        ReferenceDemo referenceDemo = new ReferenceDemo();
        List<Integer> collect2 = cars.stream().map(referenceDemo::methodReference).collect(Collectors.toList());

        //实例3、构造引用
        Supplier<ReferenceDemo> referenceDemo1 = ReferenceDemo::new;
        ReferenceDemo referenceDemo2 = referenceDemo1.get();

        //实例4、构造引用
        Function<Car, ReferenceDemo> function = ReferenceDemo::new;
        ReferenceDemo apply = function.apply(new Car());

    }


    public Integer methodReference(Car car) {
        return car.getPrice();
    }


    private static List<Car> getList() {
        Car a0 = new Car("奔驰A级", 45, "白色", 4700, Arrays.asList("宝山分店1", "浦东店"));
        Car a1 = new Car("奔驰E级", 55, "白色", 4900, Arrays.asList("宝山分店1", "宝山分店2", "浦东店"));
        Car a2 = new Car("奔驰C级", 30, "黑色", 4900, Arrays.asList("宝山分店1", "宝山分店2", "静安店", "静安分店2", "浦东店"));
        Car a3 = new Car("保时捷718", 60, "黄色", 4379, Arrays.asList("宝山分店1", "宝山分店2", "静安店", "静安分店2", "浦东店"));
        Car a4 = new Car("保时捷911", 400, "绿色", 4500, Arrays.asList("宝山分店1", "宝山分店2", "静安店", "静安分店2", "浦东店"));
        Car a5 = new Car("宝马 X1", 30, "蓝色", 4565, Arrays.asList("宝山分店1", "浦东店", "静安店", "徐汇区旗舰店", "东方明珠店"));
        Car a6 = new Car("宝马 X3", 50, "白色", 4717, Arrays.asList("宝山分店1", "浦东店", "静安店", "徐汇区旗舰店", "东方明珠店"));
        List<Car> cars = Arrays.asList(a0, a1, a2, a3, a4, a5, a6);
        return cars;
    }


    public ReferenceDemo() {

    }

    public ReferenceDemo(Car car) {

    }

}
